﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.Mvc;
using XRM.Security.DataAccess.Repositories.Impl;
using db = XRM.Security.DataAccess.Models;

namespace XRM.Security.WebUI.Models.UserRegister
{
    public class PCModel : BaseViewModel
    {
        public PCModel()
        {
            StickerList = new List<PCStickerModel>();
        }

        public PCModel(db.PC dbEntity)
        {
            StickerList = new List<PCStickerModel>();

            if (dbEntity != null)
            {
                Id = dbEntity.PCId;
                PCHost = dbEntity.PCHost;
                SerialNumber = dbEntity.SerialNumber;
                GetStickers(dbEntity.Stickers.ToList());
            }
        }

        public PCModel(db.UserPC dbEntity)
        {
            if (dbEntity != null)
            {
                Id = dbEntity.PCId;
                PCHost = dbEntity.PC.PCHost;
                SerialNumber = dbEntity.PC.SerialNumber;
                GetStickers(dbEntity.PC.Stickers.ToList());
            }
        }

        public void GetStickers(List<db.PCSticker> dbList)
        {
            StickerList = new List<PCStickerModel>();
            if (dbList != null && dbList.Any())
            {
                foreach (db.PCSticker item in dbList)
                {
                    StickerList.Add(new PCStickerModel(item));
                }
            }
        }

        [AllowHtml]
        [Display( Name = "Hostname" )]
        [Required( ErrorMessage = "Поле 'Hostname' обязательное" )]
        [MaxLength( 250, ErrorMessage = "Максимальная длина поля 'Hostname' 250 символов" )]
        public string PCHost { get; set; }

        [AllowHtml]
        [Display(Name = "Серийный номер")]
        [Required(ErrorMessage = "Поле 'Серийный номер' обязательное")]
        [MaxLength( 50, ErrorMessage = "Максимальная длина поля 'Серийный номер' 50 символов" )]
        public string SerialNumber { get; set; }

        [AllowHtml]
        [Display(Name = "Номер наклейки")]
        public List<PCStickerModel> StickerList { get; set; }

        public void ApplyChanges(db.PC dbEntity, PCStickerRepository stickerRepository)
        {
            dbEntity.PCHost = PCHost;
            dbEntity.SerialNumber = SerialNumber;
            if (dbEntity.Stickers != null)
            {
                foreach (var sticker in dbEntity.Stickers.ToList())
                {
                    stickerRepository.Delete(sticker);
                }
                dbEntity.Stickers.Clear();
            }
            dbEntity.Stickers = StickerList.Select( s => new db.PCSticker
                {
                    StickerNumber = s.StickerNumber
                } ).ToList();
        }

        public string StickerColumnData
        {
            get
            {
                if (StickerList.Any())
                    return StickerList.Select(i => i.StickerNumber).ToList().Aggregate((current, next) => current + "<br/>" + next);
                else
                    return "";
            }
        }
    }
}